package shop.controller;


import jakarta.servlet.http.HttpServletResponse;
import org.flown.shop.entity.error.ReadError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.entity.GoodsComment;
import shop.service.GoodsCommentService;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Base64;


@RequestMapping("/goods")
@Controller
public class GoodsCommentGoodsController
{
    @Autowired
    private GoodsCommentService service_goods_comment;


    @ResponseBody
    @RequestMapping("/{goods_id}/getGoodsCommentByID")
    public GoodsComment getGoodsCommentByID(@PathVariable int goods_id,
                                            @RequestParam("id") int comment_id)
    {
        GoodsComment comment = this.service_goods_comment.getGoodsCommentByID(comment_id);
        if (comment.getGoodsID() != goods_id)
            return null;
        return comment;
    }


    @RequestMapping("/{goods_id}/getGoodsCommentPictureByIDAndIndex")
    public void getGoodsCommentPictureByIDAndIndex(HttpServletResponse response,
                                                   @PathVariable int goods_id,
                                                   @RequestParam("id") int comment_id,
                                                   @RequestParam("index") int picture_index)
            throws IOException
    {
        GoodsComment comment = this.getGoodsCommentByID(goods_id, comment_id);
        if (comment == null)
        {
            PrintWriter out = response.getWriter();
            out.println("评论不存在");
            out.close();
            return;
        }
        byte[] bytes;
        try {
            bytes = comment.getPicture(picture_index);
            String format = comment.getPictureFormat(picture_index);

            switch (format) {
                case "jpg", "jpeg" -> response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                case "png" -> response.setContentType(MediaType.IMAGE_PNG_VALUE);
                case "gif" -> response.setContentType(MediaType.IMAGE_GIF_VALUE);
                default -> throw new ReadError("unknown picture format");
            }

            OutputStream out = response.getOutputStream();
            out.write(bytes);
            out.close();
        } catch (ReadError e) {
            PrintWriter out = response.getWriter();
            out.println("图片读取错误");
            out.close();
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }


    /**
     * @return 返回base64编码后的图片
     * */
    @ResponseBody
    @RequestMapping("/{goods_id}/getGoodsCommentPicturesByID")
    public String[] getGoodsCommentPicturesByID(@PathVariable int goods_id,
                                                @RequestParam("id") int comment_id)
            throws ReadError, IOException
    {
        GoodsComment comment = this.getGoodsCommentByID(goods_id, comment_id);
        if (comment == null)
            return null;
        byte[][] pictures = comment.getPictures();
        String[] base64_pictures = new String[pictures.length];
        for (int i = 0; i < pictures.length; i++)
            base64_pictures[i] = Base64.getEncoder().encodeToString(pictures[i]);
        return base64_pictures;
    }

}

